//
//  PickAPal.swift
//  AppleDemo
//
//  Created by Just 花语 on 2025/10/26.
//

import SwiftUI
struct PickAPalView: View {
    @State private var names:[String] = ["Elisha", "Andre", "Jasmine", "Po-Chun"]
    @State private var topSelect:String = ""
    @State private var addText:String = ""
    @State private var shouldRemovePickedName = false
    @State private var hoveredIndex:Int? = nil
    var body:some View{
        VStack{
            VStack(spacing:8){
                Image(systemName: "person.3.sequence.fill")
                    .foregroundStyle(.tint)
                    .font(.system(size: 40))
                Text("Pick-a-Pal")
                    .font(.largeTitle)
                    .bold()
            }
            Text(topSelect)
                .font(.system(size: 40))
                .foregroundStyle(.tint)
                .bold()
                .padding(.top,5)
            List(0..<names.count, id:\.self){ index in
                HStack{
                    Text(names[index])
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(0)
                }
                .contentShape(Rectangle())
                .background(hoveredIndex == index ? Color.blue.opacity(0.2) : Color.clear)
                .onTapGesture {
                    if hoveredIndex == index{
                        hoveredIndex = nil
                    }else{
                        hoveredIndex = index
                    }
                }
                .animation(.easeInOut(duration: 0.1), value:hoveredIndex)
                    
            }
            .clipShape(RoundedRectangle(cornerRadius: 8))
            TextField("Add Name", text: $addText)
                .onSubmit {
                    if !addText.isEmpty {
                        names.append(addText)
                        addText = ""
                    }
                }.padding(.horizontal,20)
            Divider()
            Toggle("Remove when picked",isOn: $shouldRemovePickedName)
                .padding(.horizontal,20)
            Button{
                guard !names.isEmpty else {
                    topSelect = ""
                    return
                }
                withAnimation {
                    if let randName = names.randomElement() {
                        topSelect = randName
                        if shouldRemovePickedName {
                            names.removeAll { $0 == randName }
                        }
                    }
                }
            }label: {
                Text("Pick a Pal")
                    .font(.system(size: 30))
            }
            .buttonStyle(.borderedProminent)
            .font(.title2)
            
        }
    }
}
#Preview {
    PickAPalView()
}
